﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TangoFamilyAndGuide.Site
{
    public partial class HousingReservar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Entidades.Sesion sesion;
            if (!IsPostBack)
            {
                if (Funciones.SessionTimeOut(Session))
                {
                    Response.Redirect("~/SessionTimeout.aspx");
                }
                else
                {
                    sesion = (Entidades.Sesion)Session["Sesion"];

                    Entidades.Housing Housing = (Entidades.Housing)Session["Housing"];
                    string a = HttpContext.Current.Request.Url.Query.ToString().Replace("?", String.Empty);
                    switch (a)
                    {
                        case "Id":
                            TituloPaginaLabel.Text = "Reservation of Milonguero Housing Network (Id." + Housing.Id.ToString() + ")";
                            break;
                    }

                    IdHousingTextBox.Text = Housing.Id.ToString();
                    DescripcionLabel.Text = Housing.Descripcion;
                    
                    PaisLabel.Text = Housing.Pais;
                    ProvinciaLabel.Text = Housing.Provincia;
                    LocalidadLabel.Text = Housing.Localidad;
                    CodPostLabel.Text = Housing.CodPost;

                    DireccionLabel.Text = Housing.Direccion;
                    NombreContactoLabel.Text = Housing.Contacto.Nombre;
                    EmailContactoLabel.Text = Housing.Contacto.Email;
                    TelefonoContactoLabel.Text = Housing.Contacto.Telefono;
                    GoogleMapLabel.Text = Housing.GoogleMap;

                    DireccionLabel.Visible = false;
                    NombreContactoLabel.Visible = false;
                    EmailContactoLabel.Visible = false;
                    TelefonoContactoLabel.Visible = false;
                    GoogleMapLabel.Visible = false;

                    ComentariosLabel.Text = Housing.Comentarios;
                    IdHousingTextBox.Enabled = false;

                    String path = Server.MapPath("~/ImagenesHousing/");
                    string[] archivos = System.IO.Directory.GetFiles(path, Housing.Usuario.Id + "-" + Housing.Id.ToString() + "-*.*", System.IO.SearchOption.TopDirectoryOnly);
                    CompletarImagenesActuales(archivos);

                    path = Server.MapPath("~/ImagenesHousing/Portada/");
                    archivos = System.IO.Directory.GetFiles(path, Housing.Usuario.Id + "-" + Housing.Id.ToString() + ".*", System.IO.SearchOption.TopDirectoryOnly);
                    CompletarImagenPortada(archivos);

                    SalirButton.Focus();
                    DataBind();
                }
            }
            MensajeLabel.Text = "";
            MensajeLabel.Focus();
        }
        private void CompletarImagenesActuales(string[] archivos)
        {
            Image1.ImageUrl = "~/Imagenes/Interrogacion.jpg";
            Image2.ImageUrl = "~/Imagenes/Interrogacion.jpg";
            Image3.ImageUrl = "~/Imagenes/Interrogacion.jpg";
            for (int i = 0; i < archivos.Length; i++)
            {
                if (i == 0)
                {
                    linkImage1.HRef = "~/ImagenesHousing/" + archivos[0].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                    Image1.ImageUrl = "~/ImagenesHousing/" + archivos[0].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                }
                else if (i == 1)
                {
                    linkImage2.HRef = "~/ImagenesHousing/" + archivos[1].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                    Image2.ImageUrl = "~/ImagenesHousing/" + archivos[1].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                }
                else if (i == 2)
                {
                    linkImage3.HRef = "~/ImagenesHousing/" + archivos[2].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                    Image3.ImageUrl = "~/ImagenesHousing/" + archivos[2].Replace(Server.MapPath("~/ImagenesHousing/"), String.Empty);
                }
            }
        }
        private void CompletarImagenPortada(string[] archivos)
        {
            ImagePortada.ImageUrl = "~/Imagenes/Interrogacion.jpg";
            if (archivos.Length >= 1)
            {
                linkPortada.HRef = "~/ImagenesHousing/Portada/" + archivos[0].Replace(Server.MapPath("~/ImagenesHousing/Portada/"), String.Empty);
                ImagePortada.ImageUrl = "~/ImagenesHousing/Portada/" + archivos[0].Replace(Server.MapPath("~/ImagenesHousing/Portada/"), String.Empty);
            }
        }

        protected void AceptarButton_Click(object sender, EventArgs e)
        {
            if (Funciones.SessionTimeOut(Session))
            {
                Response.Redirect("~/SessionTimeout.aspx");
            }
            else
            {
                Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                Entidades.Housing Housing = new Entidades.Housing();
                try
                {
                    Housing = (Entidades.Housing)Session["Housing"];
                    Entidades.Evento Evento;
                    RN.Housing.EventoReservar(out Evento, Housing, sesion);
                    if (RN.Housing.EstaReservado(Housing.Id, sesion))
                    {
                        MensajeLabel.Text = "This is already reserved. Select another.<br/><br/>";
                        //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('This is already reserved. Select another.');</script>");
                    }
                    else
                    {
                        RN.Housing.Reservar(Housing, Evento.Id, Evento.EstadoHst, sesion);
                        AceptarButton.Enabled = false;

                        Funciones.PersonalizarControlesMaster(Master, true, sesion);
                        MensajeLabel.Text = "Your reservation was accepted. <br/><br/>";
                    }
                }
                catch (Exception ex)
                {
                    MensajeLabel.Text = EX.Funciones.Detalle(ex) + "<br/><br/>";
                 }
                finally
                {
                    UpdatePanel2.Update();
                }
            }
        }
    }
}